﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Threading;
using System.Runtime.Remoting.Messaging;
using System.Data.Odbc;

namespace database
{
    class dbase
    {
        OdbcConnection oc = new OdbcConnection();
        OdbcDataAdapter oda = new OdbcDataAdapter();
        DataTable dt = new DataTable();
        public event EventHandler process_completed;

        public DataTable data()
        {
            return dt;
        }

        delegate void CreateThread(string tbl);
        public void GenerateConnection(string tbl)
        {
            CreateThread cThread = new CreateThread(connectionDatabase);
            AsyncCallback ac = new AsyncCallback(callbackMethod);
            IAsyncResult ar = cThread.BeginInvoke(tbl, ac, null);
        }
        void callbackMethod(IAsyncResult ar)
        {
            CreateThread cThread = (CreateThread)((AsyncResult)ar).AsyncDelegate;
            cThread.EndInvoke(ar);
            if (process_completed != null) process_completed(null, null);
        }
        public void connectionDatabase(string sql)
        {
           
                if (oc.State == ConnectionState.Closed)
                {
                    oc.ConnectionString = "Driver={MySQL ODBC 5.1 Driver} " +
                            " ;SERVER=localhost" + ";DATABASE=agape_database" +
                            "; UID=root" +
                            "; PWD=" +
                            "; OPTION=3";

                    oda.SelectCommand = new OdbcCommand(sql, oc);
                    dt.Clear();
                    dt.Reset();
                    oda.Fill(dt);
                    oc.Close();
                }
          

        }
    }
}